<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\User\Mapper;

use App\User\Model\UserBag;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 用户产品背包Mapper类
 */
class UserBagMapper extends AbstractMapper
{
    /**
     * @var UserBag
     */
    public $model;

    public function assignModel()
    {
        $this->model = UserBag::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // ID
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 用户id
        if (isset($params['user_id']) && filled($params['user_id'])) {
            $query->where('user_id', '=', $params['user_id']);
        }

        // 产品id
        if (isset($params['product_id']) && filled($params['product_id'])) {
            $query->where('product_id', '=', $params['product_id']);
        }

        // 产品名称
        if (isset($params['product_name']) && filled($params['product_name'])) {
            $query->where('product_name', 'like', '%'.$params['product_name'].'%');
        }

        // 产品价格
        if (isset($params['price']) && filled($params['price'])) {
            $query->where('price', '=', $params['price']);
        }

        // 产品图片
        if (isset($params['product_img']) && filled($params['product_img'])) {
            $query->where('product_img', 'like', '%'.$params['product_img'].'%');
        }

        // 扩展字段
        if (isset($params['extend_field']) && filled($params['extend_field'])) {
            $query->where('extend_field', 'like', '%'.$params['extend_field'].'%');
        }

        // 背包产品来源
        if (isset($params['source_type']) && filled($params['source_type'])) {
            $query->where('source_type', '=', $params['source_type']);
        }

        // 来源对应id
        if (isset($params['source_id']) && filled($params['source_id'])) {
            $query->where('source_id', '=', $params['source_id']);
        }

        // 0 入库,1 已兑换,2 已提取,3提取中,4充能消耗,5熔炼消耗,6升级消耗
        if (isset($params['status']) && filled($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // dtm事务
        if (isset($params['gid']) && filled($params['gid'])) {
            $query->where('gid', '=', $params['gid']);
        }

        // 创建者
        if (isset($params['created_by']) && filled($params['created_by'])) {
            $query->where('created_by', '=', $params['created_by']);
        }

        // 创建时间
        if (isset($params['create_time']) && filled($params['create_time'])) {
            $query->where('create_time', '=', $params['create_time']);
        }

        // 更新时间
        if (isset($params['update_time']) && filled($params['update_time'])) {
            $query->where('update_time', '=', $params['update_time']);
        }

        return $query;
    }
}